# -*- coding: utf-8 -*-
from flask import Flask

from settings import ConfigMapping
from .extentions import db


def register_extentions(app):
    """注册扩展"""
    # 注册flask-restx
    from app.api import api
    api.init_app(app)

    # 注册flask-sqlalchemy
    db.init_app(app)
    # 引入model，以便生成数据库表
    from app.models.user import User
    from app.models.commodity import Commodity
    from app.models.cart import Cart
    from app.models.order import Order
    with app.app_context():
        db.create_all()


def create_app(env='dev'):
    app = Flask(__name__)

    app.__version__ = "v1.0"

    env_config = ConfigMapping.get(env)
    app.config.from_object(env_config)

    register_extentions(app)

    return app
